from db_interface import spu_attribute_table
import time
from . import ConnPG
import psycopg2
from sanic.log import logger

class SkuAttributeTable:



    async def check_sku_attribute_detail(self,sku_id, spu_attribute_id):#添加属性时检查函数
        
        #检查sku_id,和spu_attribute_id的spu是否一致

        sql = 'select * from view_sku_info where id=%s'

        sql_ = 'select * from spu_attribute where id=%s'

        sku_info_result = await ConnPG().fetchone(sql, (sku_id,))#sku详细信息

        spu_attribute_info_result = await ConnPG().fetchone(sql_,(spu_attribute_id,))#查看attribute_id的详细信息

        if sku_info_result == None:

            return "sku_id不存在"

        if spu_attribute_info_result == None:

            return "attribute_id不存在"

        if sku_info_result['spu_id'] != spu_attribute_info_result['spu_id']:

            return 'attribute_id和sku_id不属于同一spu'

        #检查sku_id原有属性中原本就就当前spu_attribute_id

        sql__ = 'select * from view_sku_attribute where sku_id=%s'

        sku_attribute_result = await ConnPG().fetchall(sql__, (sku_id,))#查看sku所有属性

        if sku_attribute_result != []:#如果当前sku已有属性

            if spu_attribute_id in [i['attribute_id'] for i in sku_attribute_result]:

                return '属性已经存在当前sku'

            #检查sku_id原有属性中原本就就当前spu_attribute_id的attribute_key

            else:
                for i in sku_attribute_result:

                    if i['attribute_key'] == spu_attribute_info_result['attribute_key']:
            
                        delete_repetition_sql = 'delete from sku_attribute where attribute_id=%s'

                        await ConnPG().execute(delete_repetition_sql, (i['attribute_id'],))

        return '该属性可以添加'



    async def add_sku_attribute(self,sku_id, attribute_id):#添加sku属性

        #参数检查
        if not all([sku_id, attribute_id]) or not sku_id.isdecimal() or not attribute_id.isdecimal():
        # if not sku_id.isdecimal() or not attribute_id.isdecimal():

            return {
                'status': -1,
                'errorCode': 111100,
                'errorMsg': '请检查参数合法性',
                'resultBody': None
            }

        sku_id, attribute_id = int(sku_id), int(attribute_id)

        check_result = await self.check_sku_attribute_detail(sku_id, attribute_id)

        if check_result != '该属性可以添加':

            return {

                'status': -1,
                'errorCode': 111101,
                'errorMsg': check_result,
                'resultBody': None,
            }
         

        sql = 'INSERT INTO sku_attribute(sku_id, attribute_id) VALUES (%s, %s)'

        await ConnPG().execute(sql, (sku_id, attribute_id))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': "SKU属性添加成功",
        }


    async def del_sku_attribute(self,sku_attribute_id):#删除sku属性

        #参数检查
        if not bool(sku_attribute_id) or not sku_attribute_id.isdecimal():
        # if not sku_id.isdecimal() or not attribute_id.isdecimal():

            return {
                'status': -1,
                'errorCode': 111100,
                'errorMsg': '请检查参数合法性',
                'resultBody': None
            }

     
        sql = 'DELETE FROM sku_attribute WHERE id=%s'

        try:
            await ConnPG().execute(sql, (sku_attribute_id,))

        except psycopg2.errors.ForeignKeyViolation as e:

            # print(e)

            return {

                'status': -1,
                'errorCode': 111112,
                'errorMsg': str(e),
                'resultBody': None,
            }
        else:
        
            return {
                'status': 1,
                'errorCode': None,
                'errorMsg': None,
                'resultBody': "sku属性删除成功",                                     
            }






    async def select_attribute(self, sku_id):#查看指定sku_id的属性

        if not sku_id or not sku_id.isdecimal():
            return {
                'status': -1,
                'errorCode': 113100,
                'errorMsg': '请检查参数合法性',
                'resultBody': None
            }        

        sql = 'select * from view_sku_attribute where sku_id=%s'

        result = await ConnPG().fetchall(sql,(sku_id,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }

    async def select_sku_info(self, sku_id):#查看指定sku_id的信息

        if not sku_id or not sku_id.isdecimal():
            return {
                'status': -1,
                'errorCode': 112100,
                'errorMsg': '请检查参数合法性',
                'resultBody': None
            }

        sql = 'select * from view_sku_info where id=%s'

        result = await ConnPG().fetchall(sql, (sku_id,))

        return {
            'status': 1,
            'errorCode': None,
            'errorMsg': None,
            'resultBody': result,
        }
    

    